package actionHandlers;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import cuShapes.PQShape;

import palette.CanvasManager;
import palette.CanvasWindow;

public class ShapeTreeSelectListener implements MouseListener{

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// Uncomment the next line to observe when this arg0 is being handled
		//System.out.println("mouseClicked event on shapeTree's JTree");
		
		JTree tree = ((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getShapeTree().getTree();
		TreePath selPath = tree.getPathForLocation(arg0.getX(), arg0.getY());
		
		// The user can click on the JTree, but not necessarily on a TreeNode.
		// This try block catches the times the user does not click on a TreeNode, and does nothing with it.
		try {
			DefaultMutableTreeNode tempNode = (DefaultMutableTreeNode) selPath.getLastPathComponent();
			// Uncomment the next line to observe when this arg0 is being handled
			//System.out.println("mouseClicked event hit on a TreeNode");
			
			// Only care about the nodes that reference userObjects of type PQShapes. 
			// This keeps the roots from trying to change currentShape since they reference userObjects of type String. 
			if(tempNode.getUserObject() instanceof PQShape)
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().setCurrentShape((PQShape) tempNode.getUserObject());
		} catch (Exception e) {
			// No problem, the user just clicked on the JTree, but not on a TreeNode
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

//public class ShapeTreeSelectListener implements TreeSelectionListener{
//
//	@Override
//	public void valueChanged(TreeSelectionEvent arg0) {
//		DefaultMutableTreeNode tempNode = (DefaultMutableTreeNode) arg0.getPath().getLastPathComponent();
//		
//		// Only care about the nodes that reference userObjects of type PQShapes. 
//		// This keeps the roots from trying to change currentShape since they reference userObjects of type String. 
//		if(tempNode.getUserObject() instanceof PQShape)
//			((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().setCurrentShape((PQShape) tempNode.getUserObject());
//		
//		System.out.println("how many");
//	}
//}
